<?php


namespace app\admin\controller;

use app\common\controller\AdminBase;
use app\common\model\OrderlistModel;
use think\facade\Db;
class Orderlist extends AdminBase
{
    protected OrderlistModel $model;

    /**
     * 初始化
     * @return void
     */
    public function initialize()
    {
        parent::initialize();
        $this->model = new OrderlistModel();
    }

    /**
     * 订单管理列表
     */
    public function index()
    {
        $param = $this->request->param();
        $model = $this->model;
        //构造搜索条件
        if( $param['ordernum']!='' ) {
            $model = $model->whereLike('a.ordernum',"%".$param['ordernum']."%");
        }
        if( $param['start_date']!=''&&$param['end_date']!='' ) {
            $start_date = $param['a.start_date'].' 00:00:00';
            $end_date = $param['a.end_date'].' 23:59:59';
            $model = $model->whereBetweenTime('create_time',$start_date,$end_date);
        }
        $list = $model->alias('a')
		    ->join('member b','b.id=a.user_id')
			->join('schoolproduct c','c.id=a.product_id')
			->join('product d','d.id=c.product_id')
			->join('useraddress e','e.id=a.address_id')
		    // ->where(['a.platform_id'=>2])
			->order('a.create_time desc')
			->field('a.*,b.nickName,d.name as proname,e.detail as address,e.name as names')
            ->paginate([
                'query' => $param,
                'list_rows'=>$this->request->param('limit',20)
            ]);
        return view('', compact('list'));
    }

    /**
     * 添加订单管理
     */
    public function add()
    {
        if( $this->request->isPost() ) {
            $param = $this->request->param();
            $result = $this->model->save($param);
            if( $result ) {
                xn_add_admin_log('添加订单管理');
                $this->success('操作成功');
            } else {
                $this->error('操作失败');
            }
        }
        return view('form');
    }

    /**
     * 编辑订单管理
     */
    public function edit()
    {
        if( $this->request->isPost() ) { 
            $param = $this->request->param();
			// if(!empty($param['ordernum'])){
			// 	$param['deliver_status']=1;
			// }
            $result = $this->model->update($param);
            if( $result ) {
                xn_add_admin_log('编辑订单管理');
                $this->success('操作成功');
            } else {
                $this->error('操作失败');
            }
        }
		$info=Db::name('product')->select()->toArray();
		
        // return view('form',compact('info'));
        $id = $this->request->get('id');
        $_data = $this->model->find($id);
		
        $data = $_data->toArray();
		$address=Db::name('useraddress')->where('id',$data['address_id'])->find();
		$product=Db::name('schoolproduct')->where('id',$data['product_id'])->find();
		$name=Db::name('product')->where(['id'=>$product['product_id']])->find();
		$data['proname']=$name['name'];
		
        $data['address']=$address['detail'];
        return view('form',compact('data','info'));
    }

    /**
     * 删除订单管理
     */
    public function delete()
    {
        $id = $this->request->param('id');
        $this->model->destroy($id);
        xn_add_admin_log('删除订单管理');
        $this->success('删除成功');
    }
}